<template>
  <div class="point-list">
    <div v-if="orderList.length === 0" class="no-img">
      <div></div>
      <span>无订单</span>
    </div>
    <template v-else>
      <div class="order" v-for="item in orderList" :key="item.id">
        <div class="top">
          <div class="code">{{ "订单号 " + item.integralOrderNo }}</div>
          <div class="status">
            {{ item.writeOffStatus === 1 ? "已核销" : "已兑换" }}
          </div>
        </div>
        <div class="middle" @click="gotoHexiao(item)">
          <div class="imgcon">
            <img :src="item.waresPic" alt="" srcset="" />
          </div>
          <div class="content" style="position:relative">
            <div class="title">{{ item.waresName }}</div>
            <div class="desc">{{ item.waresDetail }}</div>
            <div class="amount">
              <div>
                <span style="color:#999;font-size:0.26rem">
                  兑换时间：{{ item.exchangeTime }}
                </span>
                <br />
                <span
                  style="color:#999;font-size:0.26rem"
                  v-if="item.writeOffTime"
                  >核销时间：{{ item.writeOffTime }}</span
                ><br />
                <span class="text">共{{ item.exchangeNum }}件商品 合计 </span>
                <span class="icon"></span><span>{{ item.integral }}</span>
              </div>
            </div>
            <!-- 二维码  couponOrNot: 0 代表商品  1 代表这个商品就是优惠券   -->
            <div class="codeimg" v-if="item.couponOrNot === 0">
              <img
                style="max-width:100%"
                src="@/static/coupon/showqrcode.png"
              />
              <br />
              <span>点击展示核销码</span>
            </div>
          </div>
        </div>

        <!-- 二维码 -->
        <van-popup v-model="isShowCode">
          <qr :text="scanCodeText" name="二维码"></qr>
        </van-popup>

        <!-- <div class="footer">
        <span class="paytime">付款倒计时：23:59:59</span>
        <div class="paybutton">
          <button class="cancel">取消订单</button>
          <button class="pay">立刻付款</button>
        </div>
      </div> -->
      </div>
    </template>
    <!-- <div class="order">
      <div class="top">
        <div class="code">订单号 836847623234289</div>
        <div class="status">待付款</div>
      </div>
      <div class="middle">
        <div class="imgcon">
          <img :src="listsrc" alt="" srcset="" />
        </div>
        <div class="content">
          <div class="title">水光气垫</div>
          <div class="desc">15g SPF 50+ PA +++ 自然白</div>
          <div class="amount">
            <div>
              <span class="text">共1件商品 合计 </span><span class="icon"></span
              ><span>300</span>
            </div>
          </div>
        </div>
      </div>

      <div class="footer">
        <span class="paytime">付款倒计时：23:59:59</span>
        <div class="paybutton">
          <button class="cancel">取消订单</button>
          <button class="pay">立刻付款</button>
        </div>
      </div>
    </div>
    -->
  </div>
</template>

<script>
import Vue from "vue";
import { Popup } from "vant";
import ProductList from "@/components/ProductList";
import Qr from "@/components/SpCode";

Vue.use(Popup);
export default {
  name: "PointList",
  head() {
    return {
      title: "积分订单"
    };
  },
  components: {
    ProductList,
    Qr
  },
  data() {
    return {
      orderList: [],
      scanCodeText: "",
      isShowCode: false,
      shopInfo: {}
    };
  },
  mounted() {
    this.initOrderList();
  },
  watch: {
    isShowCode(val) {
      let integralOrderNo = this.shopInfo.integralOrderNo;
      let phone = this.phone;
      if (val) {
        this.timer = setInterval(() => {
          console.log("核销完成了吗");
          this.$axios
            .$get("/getMerchandiseOrderWrittenOff", {
              params: {
                integralOrderNo: integralOrderNo,
                mobilePhone: phone
              }
            })
            .then(res => {
              if (res.retCode === 0) {
                console.log("核销成功", res);
                clearInterval(this.timer);
                this.$router.push(
                  `/${this.enterpriseId}/point/verification_Sp?writeOffTime=${res.data.writeOffTime}`
                );
              }
            })
            .catch(err => {
              clearInterval(this.timer);
              alert(err + "核销失败");
            });
        }, 2000);
      } else {
        clearInterval(this.timer);
      }
    }
  },
  methods: {
    initOrderList() {
      this.$axios
        .$get("/getUserIntegralOrderList", {
          params: {
            mobilePhone: this.phone,
            enterpriseId: this.enterpriseId
          }
        })
        .then(res => {
          if (res.retCode === 0) {
            this.orderList = res.data;
          }
        });
    },
    // 商品券核销
    gotoHexiao(item) {
      if (item.writeOffStatus) {
        // writeOffStatus   0为未核销 1为已核销
        return alert("已核销");
      } else if (item.couponOrNot) {
        // couponOrNot: 0 代表商品  1 代表这个商品就是优惠券
        return alert("暂不支持核销");
      } else {
        // 只有属于商品 且 未核销的才能展示二维码
        this.shopInfo = item;
        this.scanCodeText = "sp_" + item.integralOrderNo;
        this.isShowCode = true;
        console.log("核销码参数：", this.scanCodeText);
      }
    }
  },
  beforeDestroy() {
    console.log("组件销毁");
    clearInterval(this.timer);
  }
};
</script>

<style scoped lang="scss">
.point-list {
  width: 100%;
  background: #f8f8f8;
  width: 7rem;
  margin: 0 auto;
}
.point-list .no-img {
  width: 100%;
  height: 60vh;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.point-list .no-img div {
  width: 2.29rem;
  height: 1.87rem;
  background: url("../../../static/common/wu@2x.png") no-repeat 0 0;
  background-size: 100% 100%;
}
.point-list .no-img span {
  width: 2.29rem;
  height: 0.4rem;
  font-size: 0.28rem;
  font-weight: 500;
  color: #aaaaaa;
  line-height: 0.4rem;
  text-align: center;
}
.point-list .order {
  width: 100%;
  background: #ffffff;
  margin-top: 0.2rem;
}
.point-list .order .top {
  width: 100%rem;
  height: 1rem;
  position: relative;
  border-bottom: 0.01rem solid #e0e0e0;
}
.point-list .order .top .code {
  height: 0.4rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
  line-height: 0.4rem;
  position: absolute;
  top: 0.3rem;
  left: 0.3rem;
}
.point-list .order .top .status {
  height: 0.4rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
  line-height: 0.4rem;
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
}

.point-list .order .middle {
  width: 100%;
  height: 5rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
.point-list .order .middle .imgcon {
  width: 2.4rem;
  height: 2.4rem;
  background: #ffffff;
  border-radius: 0.1rem;
  border: 0.02rem solid #eeeeee;
  box-sizing: border-box;
  padding: 0.3rem;
  margin-bottom: 1rem;
}
.point-list .order .middle .imgcon img {
  width: 100%;
  height: 100%;
}
.point-list .order .middle .content {
  width: 4.48rem;
  height: 3.4rem;
  padding: 0.3rem 0.1rem;
}
.point-list .order .middle .content .title {
  width: 3.1rem;
  height: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #111111;
  line-height: 0.45rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.point-list .order .middle .content .desc {
  width: 3rem;
  height: 1.95rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
  line-height: 0.4rem;
  word-break: break-all;
  overflow: hidden;
}
.point-list .order .middle .content .amount {
  width: 4.2rem;
  height: 1rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #fe9700;
  line-height: 0.45rem;
  position: relative;
}
.point-list .order .middle .content .amount div {
  display: inline-block;
  position: absolute;
  right: 0;
  top: 0.2rem;
  text-align: right;
  text-align: end;
}
.point-list .order .middle .content .amount .text {
  width: 1.7rem;
  height: 0.33rem;
  font-size: 0.24rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
  line-height: 0.33rem;
}
.point-list .order .middle .content .amount .icon {
  display: inline-block;
  width: 0.25rem;
  height: 0.25rem;
  background: url("../../../static/common/a01_jifen@2x.png") no-repeat 0 0;
  background-size: 100% 100%;
}

.point-list .order .middle .content .codeimg {
  position: absolute;
  top: 0rem;
  right: 0rem;
  padding-top: 0.3rem;
  text-align: center;
  img {
    width: 1rem;
  }
  span {
    font-size: 0.2rem;
  }
}

.point-list .order .footer {
  width: 100%;
  height: 1rem;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.point-list .order .footer .paytime {
  display: inline-block;
  width: 2.84rem;
  height: 0.4rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #aaaaaa;
  line-height: 0.4rem;
}
.point-list .order .footer .paybutton {
  display: flex;
  justify-content: space-between;
  width: 3.4rem;
}

.point-list .order .footer .paybutton .cancel {
  border: none;
  background-color: transparent;
  outline: none;
  display: block;
  width: 1.6rem;
  height: 0.6rem;
  border-radius: 0.3rem;
  border: 0.02rem solid #aaaaaa;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #aaaaaa;
  line-height: 0.4rem;
}
.point-list .order .footer .paybutton .pay {
  border: none;
  background-color: transparent;
  outline: none;
  display: block;
  width: 1.6rem;
  height: 0.6rem;
  background: #fe9700;
  border-radius: 0.3rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 0.4rem;
}
</style>
